﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using CMS.Model;
using CMSWCSFSolution.CMS;
using CMS.ViewModel;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using CMS.Logging;
using System.Text;

namespace CMSWCSFSolution.CMS.Views
{
    public partial class StudentMainPage :BasePage, IStudentMainPageView
    {
        #region Const Variable
        private const string CURRENT_PAGE = "CurrentPage";
        private const string ROW_COUNT = "RowCount";
        #endregion

        #region Ptotected Method
        private StudentMainPagePresenter _presenter;
        [CreateNew]
        public StudentMainPagePresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            
            try
            {
                PageSize = 5;
                if (!this.IsPostBack)
                {
                    ViewState[CURRENT_PAGE] = 1;
                    BindStudentInfo();
                }
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.Highest);
                ShowMessage(3,"error");
            }

        }
        /// <summary>
        /// Binds the student info.
        /// </summary>
        protected void BindStudentInfo()
        {
            try
            {
                _presenter.onSelectAllStudents();
                SetButtonState(PageIndex);
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.Highest);
                ShowMessage(3, "error");
            }
        }
        /// <summary>
        /// Sets the state of the button.
        /// </summary>
        /// <param name="pageIndex">Index of the page.</param>
        protected void SetButtonState(int pageIndex)
        {
            if (PageCount <= 1)
            {
                btnFirst.Visible = false; ;
                btnPre.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;
                lblPagesIndex.Visible = false;
                txtSkipPage.Visible = false;
                btnSkip.Visible = false;
            }
            else if (pageIndex == 1 && PageCount > 1)
            {
                btnFirst.Visible = false; ;
                btnPre.Visible = false;
                btnNext.Visible = true;
                btnLast.Visible = true;
                lblPagesIndex.Visible = true;
                txtSkipPage.Visible = true;
                btnSkip.Visible = true;
                Label2.Visible = false;
            }
            else if (PageCount > 1 && pageIndex > 1 && pageIndex < PageCount)
            {
                btnFirst.Visible = true; ;
                btnPre.Visible = true;
                btnNext.Visible = true;
                btnLast.Visible = true;
                lblPagesIndex.Visible = true;
                txtSkipPage.Visible = true;
                btnSkip.Visible = true;
            }
            else
            {
                btnFirst.Visible = true; ;
                btnPre.Visible = true;
                btnNext.Visible = false;
                btnLast.Visible = false;
                lblPagesIndex.Visible = true;
                txtSkipPage.Visible = true;
                btnSkip.Visible = true;
            }
            lblRowsCount.Text = string.Format("Total Items of Student is :{0}", RowCount);
            lblPagesIndex.Text = string.Format("{0}/{1}", PageIndex, PageCount);
        }
        /// <summary>
        /// Handles the Click event of the PageNavaigage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void PageNavaigage_Click(Object sender, EventArgs e)
        {
            LinkButton btn = (LinkButton)sender;
            string cmdName = btn.CommandName;
            switch (cmdName)
            {
                case "First":
                    ViewState[CURRENT_PAGE] = 1;
                    break;
                case "Previous":
                    ViewState[CURRENT_PAGE] = (int)ViewState[CURRENT_PAGE] - 1;
                    break;
                case "Next":
                    ViewState[CURRENT_PAGE] = (int)ViewState[CURRENT_PAGE] + 1;
                    break;
                case "Last":
                    ViewState[CURRENT_PAGE] = PageCount;
                    break;
            }
            BindStudentInfo();
        }
        /// <summary>
        /// Handles the Click event of the btnSkip control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSkip_Click(object sender, EventArgs e)
        {
            //if (SkipPage == -100)
            //{
            //    return;
            //}
            //else
            //{
                if (SkipPage >= PageCount)
                {
                    ViewState[CURRENT_PAGE] = PageCount;
                }
                else if (SkipPage <= 1)
                {
                    ViewState[CURRENT_PAGE] = 1;
                }
                else
                {
                    this.PageIndex = SkipPage;
                    ViewState[CURRENT_PAGE] = PageIndex;
                }
                txtSkipPage.Text = string.Empty;
            //}
            BindStudentInfo();
        }
        /// <summary>
        /// Handles the DeleteStudent event of the btn control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btn_DeleteStudent(object sender, EventArgs e)
        {

            try
            {
                StudentId = Int32.Parse(hfStudentId.Value);
                if (PageIndex == PageCount && RowCount % 5 == 1)
                {
                    _presenter.OnDeleteStudentById();
                    PageIndex = PageIndex - 1;
                }
                else
                {
                    _presenter.OnDeleteStudentById();
                }

                if (PageIndex > PageCount)
                {
                    PageIndex = PageCount;
                }
                else if (PageIndex <= 1)
                {
                    PageIndex = 1;
                }
                BindStudentInfo();
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.High);
                ShowMessage(3, "error");
            }
        }
        /// <summary>
        /// Handles the RowEditing event of the gvStudentInfo control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewEditEventArgs"/> instance containing the event data.</param>
        protected void gvStudentInfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string studentId = this.gvStudentInfo.DataKeys[e.NewEditIndex].Value.ToString();
            Response.Redirect("UpdateStudent.aspx?id=" + studentId);
        }
        /// <summary>
        /// Handles the Click event of the btnaddStudent control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnaddStudent_Click(object sender, EventArgs e)
        {
            Response.Redirect("CreateStudent.aspx");
        }
        #endregion

        #region Public Property
        /// <summary>
        /// Sets the bind student list.
        /// </summary>
        /// <value>
        /// The bind student list.
        /// </value>
        public List<StudentCourseDetailView> BindStudentList
        {
            set
            {
                this.gvStudentInfo.DataSource = value;
                this.gvStudentInfo.DataBind();
            }
        }
        /// <summary>
        /// Gets or sets the student id.
        /// </summary>
        /// <value>
        /// The student id.
        /// </value>
        public int StudentId
        {
            get;
            set;
        }
        /// <summary>
        /// Gets the page count.
        /// </summary>
        public int PageCount
        {
            get { return (RowCount + PageSize - 1) / PageSize; }
        }
        /// <summary>
        /// Gets or sets the row count.
        /// </summary>
        /// <value>
        /// The row count.
        /// </value>
        public int RowCount
        {
            get { return (int)ViewState[ROW_COUNT]; }
            set { ViewState[ROW_COUNT] = value; }
        }
        /// <summary>
        /// Gets or sets the size of the page.
        /// </summary>
        /// <value>
        /// The size of the page.
        /// </value>
        public int PageSize
        {
            get;
            set;
        }
        /// <summary>
        /// Gets or sets the index of the page.
        /// </summary>
        /// <value>
        /// The index of the page.
        /// </value>
        public int PageIndex
        {
            get
            {
                return (int)ViewState[CURRENT_PAGE];
            }
            set
            {
                ViewState[CURRENT_PAGE] = value;
            }
        }
        /// <summary>
        /// Gets the skip page.
        /// </summary>
        public int SkipPage
        {
            get
            {
                if (txtSkipPage.Text.Trim() != string.Empty)
                {
                    return Convert.ToInt32(txtSkipPage.Text.Trim());
                }
                else return -100;
            }
        }
        #endregion

    }
}

